package com.etc.togo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.etc.togo.domain.User;
import com.etc.togo.service.IUserService;
import com.etc.togo.vo.ResultVO;

@RestController
@RequestMapping("/")
@SessionAttributes("user")
public class UserController {
	@Autowired
	private IUserService userService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;
	private ResultVO result = new ResultVO();
	private Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@RequestMapping(value = "login")
	public ResultVO login(String username, String password) {
		logger.info(username + " " + password);
		result = userService.login(username, password);
		if (result.getStatus().equals("200")){
			User user=(User) result.getData();
			request.getSession().setAttribute("user", user);
			logger.info("user:"+request.getSession().getAttribute("user"));
		}
		response.setHeader("Access-Control-Allow-Origin", "*");
		return result;
	}

	@RequestMapping(value = "register")
	public ResultVO register(User user) {
		request.getSession();
		response.setHeader("Access-Control-Allow-Origin", "*");
		logger.info(user.getUsername());
		result = userService.register(user);
		return result;
	}

	@RequestMapping(value = "logout")
	public void logout() {
		request.getSession().removeAttribute("user");
		response.setHeader("Access-Control-Allow-Origin", "*");
	}

	@RequestMapping(value = "updateUser")
	public ResultVO update(User user) {
		result=userService.update(user);
		response.setHeader("Access-Control-Allow-Origin", "*");
		return result;
	}

	@RequestMapping("listUserAll")
	public ResultVO listUserAll() {
		response.setHeader("Access-Control-Allow-Origin", "*");
		result.setStatus("201");
		result.setInfo("获取失败");
		result.setData(null);
		List<User> userList = userService.getAll(User.class);
		if (userList != null) {
			if (userList.size() > 0) {
				result.setStatus("200");
				result.setInfo("获取成功");
				result.setData(userList);
			}
		}
		return result;
	}

	@RequestMapping("deleteUser")
	public ResultVO delete(Long id) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		return userService.delete(User.class, id);
	}

	@RequestMapping("getUser")
	public ResultVO getUser(String username){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return userService.getByName(username);
	}
	
	@RequestMapping("getSession")
	public ResultVO getSession(){
		HttpSession session=request.getSession();
		
		if(session.isNew()) {
			logger.info("session is new");;
		}
		logger.info("session is not null");
		User data=(User) session.getAttribute("user");
		if (data!=null)
			logger.info(data.toString());
		else
			logger.info("request.getSession() is null");
		result.setResult("201", "获取request.getSession()失败", null);
		if (data!=null)
			result.setResult("200", "获取request.getSession()成功", data);
		response.setHeader("Access-Control-Allow-Origin", "*");
		return result;
	}
	
	@RequestMapping("changePassword")
	public ResultVO changePassword(String username,String oldPassword,String newPassword){
		request.getSession();
		response.setHeader("Access-Control-Allow-Origin", "*");
		result=userService.changePassword(username,oldPassword,newPassword);
		return result;
	}
	
	@RequestMapping("changePhoneNumber")
	public ResultVO changePhoneNumber(Long user_id,String phoneNumber){
		response.setHeader("Access-Control-Allow-Origin", "*");
		logger.info("user_id:"+user_id);
		return userService.changePhoneNumber(user_id,phoneNumber);
	}
	
	@RequestMapping("changeEmail")
	public ResultVO changeEmail(Long user_id,String email){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return userService.changeEmail(user_id,email);
	}
}
